package com.sanrenzu.everybodytv.ui.recommend.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sanrenzu.everybodytv.R;
import com.sanrenzu.everybodytv.bean.sms.RecBean;
import com.sanrenzu.everybodytv.bean.sms.RecSubBean;
import com.sanrenzu.everybodytv.ui.recommend_live.VideoViewActivity;
import com.sanrenzu.everybodytv.widget.MyGirdView;

import java.util.List;

/**
 * Created by asus on 2016/9/6.
 */
public class ListViewCommendAdapter extends BaseAdapter {

    private Context mContext;
    private List<RecSubBean> list;
    private LayoutInflater inflater;

    public ListViewCommendAdapter(Context mContext, List<RecSubBean> list) {
        this.mContext = mContext;
        this.list = list;
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder = null;
        if(convertView==null) {
            convertView = inflater.inflate(R.layout.listview_item_commend, parent, false);
            mHolder = new ViewHolder(convertView);
            convertView.setTag(mHolder);
        }else{
            mHolder = (ViewHolder) convertView.getTag();
        }
        mHolder.textViewItemListName.setText(list.get(position).getName());
        List<RecBean> recBeanList = list.get(position).getRecBeanList();
        RecommendGirdViewAdapter adapter = new RecommendGirdViewAdapter(mContext,recBeanList);
        mHolder.girdViewItemListCommend.setAdapter(adapter);

        mHolder.girdViewItemListCommend.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(mContext,"GirdView点击啦",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(mContext, VideoViewActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("url",list.get(position).getRecBeanList().get(0).getUid());
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
        return convertView;
    }

    static class ViewHolder {
        ImageView imageviewJingcai;
        TextView textViewItemListName;
        ImageView imageView2Jincai;
        MyGirdView girdViewItemListCommend;

        ViewHolder(View view) {
            imageviewJingcai = (ImageView) view.findViewById(R.id.imageview_jingcai);
            textViewItemListName = (TextView) view.findViewById(R.id.textView_item_list_Name);
            imageView2Jincai = (ImageView) view.findViewById(R.id.imageView2_jincai);
            girdViewItemListCommend = (MyGirdView) view.findViewById(R.id.girdView_item_list_commend);
        }
    }
}
